package homework.work2;

import java.util.Arrays;
import java.util.Comparator;

public class Main {
    public static void main(String[] args) {
        //数据
        Cat c = new Cat(1, "小黄", 100);
        Cat c1 = new Cat(2, "小黑", 200);
        Cat c2 = new Cat(2, "貉", 2000);
        Cat c3 = new Cat(2, "马云的猫", 10000);
        Cat c4 = new Cat(2, "云哥", 8000);
        Cat c5 = new Cat(3, "吉祥物", 800);
        Cat c6 = new Cat(2, "阿修罗", 9000);
        Cat c7 = new Cat(2, "云堇", 300);
        Cat c8 = new Cat(2, "咪咪", 400);
        Cat c9 = new Cat(8, "养乐多", 500);
        Cat[] cats = {c, c1, c2, c3, c4, c5, c6, c7, c8, c9};

        //排序、使用cat类中的comparable接口
        Arrays.sort(cats);
        System.out.println(Arrays.toString(cats));

        //排序、使用匿名内部类
        Arrays.sort(cats, new Comparator<Cat>() {
            @Override
            public int compare(Cat o1, Cat o2) {
                return o1.name.compareTo(o2.name);
            }
        });
        System.out.println(Arrays.toString(cats));
        System.out.println("-----------");

        //排序、使用comparator自定义比较器
        Arrays.sort(cats,new MyComparator());
        System.out.println(Arrays.toString(cats));

        //综合排序
        Arrays.sort(cats, new Comparator<Cat>() {
            @Override
            public int compare(Cat o1, Cat o2) {
                if(o1.age!=o2.age){
                    return o1.age-o2.age;
                }
                if(o1.name.length()-o2.name.length()!=0){
                    return o1.name.length()-o2.name.length();
                }
                return Double.compare(o1.price,o2.price);
            }
        });
    }
}
